package com.binfenhulian.chinaenergy.ces.manage.controller.index;

import com.binfenhulian.chinaenergy.ces.manage.bean.param.SessionParam;
import com.binfenhulian.chinaenergy.ces.manage.core.db.po.def.AccountPO;
import com.binfenhulian.chinaenergy.ces.core.bean.result.AjaxResult;
import com.binfenhulian.chinaenergy.ces.manage.constants.Constants;
import com.binfenhulian.chinaenergy.ces.manage.framework.interceptor.ManageLoginCheck;
import com.binfenhulian.chinaenergy.ces.manage.service.account.AccountService;
import com.binfenhulian.chinaenergy.ces.utils.string.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/manage")
public class IndexController {

    @Resource(name = "com.binfenhulian.chinaenergy.ces.manage.service.account.AccountServiceImpl")
    AccountService accountService;

    @RequestMapping("/login.html")
    public String loginHtml() {
        return "manage/login";
    }

    //接口登录
    @RequestMapping("/login")
    @ResponseBody
    public Object login(HttpServletRequest request,
                        HttpServletResponse response,
                        HttpSession session,
                        String mobile,
                        String password,
                        String redirectUrl) throws IOException {
        // 假登录
        SessionParam sp = new SessionParam();
        Boolean validate = accountService.validatePassword(mobile, password);
        if (!validate) {
            return new AjaxResult(0, "用户名或密码错误", false, "");
        }

        // 设置session
        AccountPO accountPO = accountService.getAccountByMobile(mobile);
        sp.setName(accountPO.getName());
        sp.setId(accountPO.getId());
        session.setAttribute(Constants.MANAGE_SESSION_KEY, sp);

        if (StringUtils.isNotEmpty(redirectUrl)) {
            return new AjaxResult(0, "登陆成功", true, redirectUrl);
        }
        return new AjaxResult(0, "登陆成功", true, "/manage/home.html");
    }

    @RequestMapping("/logout.html")
    public Object logoutHtml(HttpSession session, String redirectUrl) {
        session.removeAttribute(Constants.MANAGE_SESSION_KEY);
        String rUrl = "redirect:/manage/login.html";
        if (redirectUrl != null) {
            rUrl = "redirect:/manage/login.html?redirectUrl="+redirectUrl;
        }
        return rUrl;
    }

    //接口登出
    @RequestMapping("/logout")
    @ResponseBody
    public Object logout(HttpSession session, String redirectUrl) {
        session.removeAttribute(Constants.MANAGE_SESSION_KEY);
        return redirectUrl;
    }

    @ManageLoginCheck
    @RequestMapping("/home.html")
    public String index(HttpSession session, String redirectUrl) {
        return "manage/home";
    }
}
